import java.util.Scanner;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Calendar;
import java.util.Random;

class createData{

	private int classNameSizeX=2,classNameSizeY=40,classNameSizeZ=8,classSize[] = new int[8];
	private int courseRecordX=4,courseRecordY;
	private int resX=5,resY=3000;
	private int resTop=0;

	private String[][][] className;		//班级表：班级，学生记录:学号，姓名
	private String[] cn = {"网络工程15-1","网络工程15-2","网络工程15-3","网络工程15-4",
						"网络工程14-1","网络工程14-2","网络工程14-3","网络工程13-3"};						//班级转换对照表
	private String[][] courseRecord;		//课程记录:id(课程记录表中的ID)，节次，周几，班级
	private String[][] res = new String[resY][resX];				//考勤记录: 日期，节次，旷课，学号，id
	private String[] status= {"旷课","请假","迟到"};

	private InputStream[] inputclass = new InputStream[8];
	private InputStream readrecord;

	private int month = 9,day = 12,year = 2016;

	public createData(){
		init();
	}
	private void init(){
		prepareData();
		initArraySize();
		prepareData();
		readToRam();		
	}

	private void prepareData(){
		try
		{
			inputclass[0] = new FileInputStream("151.txt");
			inputclass[1] = new FileInputStream("152.txt");
			inputclass[2] = new FileInputStream("153.txt");
			inputclass[3] = new FileInputStream("154.txt");
			inputclass[4] = new FileInputStream("141.txt");
			inputclass[5] = new FileInputStream("142.txt");
			inputclass[6] = new FileInputStream("143.txt");
			inputclass[7] = new FileInputStream("133.txt");
		}
		catch(Exception e){
			System.out.println("读取班级信息失败！");
		}

		try
		{
			readrecord = new FileInputStream("xxx.txt");
		}
		catch(Exception e){
			System.out.println("读取课表信息失败！");
		}
	}

	private void initArraySize(){
		try
		{
			for(int i=0;i<classNameSizeZ;i++){
				Scanner sc = new Scanner(inputclass[i]);
				int size=0;
				while(sc.hasNextLine()){
					size++;
					sc.nextLine();
				}
				classSize[i] = size;
			}

			Scanner sc = new Scanner(readrecord);
			int size=0;
		    while(sc.hasNextLine()){
					size++;
					sc.nextLine();
			}
			courseRecordY = size;
		}

		catch(Exception e){
			System.out.println("初始化班级人数失败");
		}

		className = new String[classNameSizeZ][classNameSizeY][classNameSizeX];
		courseRecord = new String[courseRecordY][courseRecordX]; 	
	}

	private void readToRam(){
		String str=null;
		try
		{	
			for(int i=0;i<classNameSizeZ;i++){
				Scanner sc = new Scanner(inputclass[i]);
				int j=0;
				while(sc.hasNextLine()){
					str = sc.nextLine();
					className[i][j++] = str.split("\t");
				}
			}
		}

		catch(Exception e){
			System.out.println("加载班级信息失败！");
		}

		try
		{
			Scanner sc = new Scanner(readrecord);
			int i=0;
			while(sc.hasNextLine()){
				str = sc.nextLine();
				//System.out.println(str);
				courseRecord[i++] = str.split("\t");
			}
		}

		catch(Exception e){
			System.out.println("加载课表信息失败！");
		}
	}

	private String createDate(){
		Calendar d = Calendar.getInstance();
		if(day>30){
			month++;
			day=1;
		}
		if(month>12){
			year++;
			month = month%12;
		}
		d.set(year,month,day);
		String str = d.get(Calendar.YEAR)+"-"+d.get(Calendar.MONTH)+"-"+d.get(Calendar.DATE);
		return str;
	}

	private int getDay(){
		int w=0,c=0,y=0,m=0,d=0;
	    c=year/100;
	    y=year%100;
	    if(month<3){
			m=month+12;
			y--;
	    }
	    else m=month;
	    d=day;
	    return (y+y/4+c/4-2*c+26*(m+1)/10+d-1)%7;
	}

	private String select(String s1,String s2,String s3){
		String id=null;
		for(int i=0;i<courseRecordY;i++){
			if(courseRecord[i][1].equals(s1) && courseRecord[i][2].equals(s2) && courseRecord[i][3].equals(s3))
				return courseRecord[i][0];
		}
		return "0";
	}

	private void insert(String id,int classes,int jieci){
		Random r = new Random();
		int[] xxx = new int[5];
		
			int x=r.nextInt(classSize[classes]-1);
			if(x+8<classSize[classes]){
				xxx[0] = x;
				xxx[1] = x+2;
				xxx[2] = x+4;
				xxx[3] = x+5;
				xxx[4] = x+6;
			}
			else{
				xxx[0] = x;
				xxx[1] = x-7;
				xxx[2] = x-5;
				xxx[3] = x-4;
				xxx[4] = x-1;
			}
		String date = createDate();
		for(int i=0;i<5;i++){
			res[resTop][0]=date;
			res[resTop][1]=jieci+"";
			res[resTop][2]=status[r.nextInt(3)];
			res[resTop][3]=className[classes][xxx[i]][0];
			res[resTop++][4]=id;
		}
	}

	private void mannufacture(){
		int i = 0,d;
		while(i++<75){
			int classes=0,jieci=0;
			String id;
			d=getDay();
			while(jieci++<4){
				while(classes<8){
					id=select(jieci+"",d+"",cn[classes]);
					if(id.equals("0")){
						classes++;
						continue;
					}
					insert(id,classes++,jieci);
				}
				classes=0;
			}
			day++;
		}
	}

	private void printRes(){
		for(int i=0;i<resTop;i++){
			System.out.println(res[i][0]+","+res[i][1]+","+res[i][2]+","+res[i][3]+","+res[i][4]+"\n");
		}
	}

	private void writeData(){
		try(
			FileOutputStream f = new FileOutputStream("result.txt");
			PrintStream ps = new PrintStream(f))
		{
			for(int i=0;i<resTop;i++){
				ps.println(res[i][0]+","+res[i][1]+","+res[i][2]+","+res[i][3]+","+res[i][4]);
			}
		}

		catch(Exception e){
			System.out.println("写入文件失败！");
		}
	}
	
	public static void main(String[] args){
		createData cd = new createData();
		cd.mannufacture();
		System.out.println(cd.resTop);
		cd.printRes();
		cd.writeData();
	}
}
